package com.yy.ptrdemo.library;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Interpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import com.yy.ptrdemo.library.utils.CustomEnums;

/**
 * Created by Ayanami_Rei on 2014/10/22.
 *
 * Base pull to refresh view
 */
public abstract class PullToRefreshBase<T extends View> extends LinearLayout implements IPullToRefresh<T> {

    public PullToRefreshBase(Context context) {
        super(context);
    }

    public PullToRefreshBase(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * Listener thar allows you to be notified when the user has start or
     * finish a touch event. Useful when you want to append extra UI events
     * (such as sounds).
     */
    public static interface OnPullEventListener<V extends View> {

        /**
         * Called when the internal state has been changed, usually by
         * the user pulling
         *
         * @param refreshView View which has had it is state change
         * @param state The new state of view
         * @param direction One of {@link com.yy.ptrdemo.library.utils.CustomEnums.Mode#PULL_FROM_START}
         *                  or{@link com.yy.ptrdemo.library.utils.CustomEnums.Mode#PULL_FROM_END}
         *                  depending on which direction the user is pulling. Only useful when <var>state</var>
         *                  is {@link com.yy.ptrdemo.library.utils.CustomEnums.State#PULL_TO_REFRESH}
         *                  or {@link com.yy.ptrdemo.library.utils.CustomEnums.State#RELEASE_TO_REFRESH}
         */
        public void onPullEvent(final PullToRefreshBase<V> refreshView, CustomEnums.State state,
                                CustomEnums.Mode direction);
    }

    /**
     * Simple listener for any callbacks for refresh
     */
    public static interface OnRefreshListener<V extends View> {

        /**
         * onRefresh() will be called for one of pull_from_start and pull_from_end
         */
        public void onRefresh(final PullToRefreshBase<V> refreshView);
    }

    /**
     * When you want to use both of pull_from_start and pull_from_end, you will use
     * this listener to callbacks for refresh
     */
    public static interface OnRefreshListenerBoth<V extends View> {

        /**
         * onPullFromStartRefresh() will be called for pull_from_start
         */
        public void onPullFromStartRefresh(final PullToRefreshBase<V> refreshView);

        /**
         * onPullFromEndRefresh() will be called for pull_from_end
         */
        public void onPullFromEndRefresh(final PullToRefreshBase<V> refreshView);
    }

    //===========================================================================
    // Constants
    //===========================================================================

    private static final boolean DEBUG = true;
    private static final boolean USE_HM_LAYERS = false;

    private static final float FRICTION = 2.0f;

    private static final int SMOOTH_SCROLL_DURATION_MS = 200;
    private static final int SMOOTH_SCROLL_LONG_DURATION_MS = 325;
    private static final int DEMO_SCROLL_INTERVAL = 225;

    private static final String TAG = "PullToRefresh";
    private static final String PTR_STATE = "ptr_state";
    private static final String PTR_MODE = "ptr_mode";
    private static final String PTR_CURRENT_MODE = "ptr_current_mode";
    private static final String PTR_SCROLLING_REFRESHING_ENABLE = "ptr_scrolling_refreshing_enable";
    private static final String PTR_SHOW_REFRESHING_VIEW = "ptr_show_refreshing_view";
    private static final String PTR_SUPER = "ptr_super";

    //===========================================================================
    // Fields
    //===========================================================================

    private int mTouchSlop;

    private float mLastMotionX, mLastMotionY;
    private float mInitialMotionX, mInitialMotionY;

    private boolean mIsBeingDragged = false;
    private boolean mShowViewWhileRefreshing = true;
    private boolean mScrollingWhileRefreshingEnabled = false;
    private boolean mFilterTouchEvents = true;
    private boolean mOverScrollEnabled = true;
    private boolean mLayoutVisibilityChangesEnabled = true;

    private CustomEnums.State mState = CustomEnums.State.RESET;

    private CustomEnums.Mode mMode = CustomEnums.Mode.getDefaultMode();
    private CustomEnums.Mode mCurrentMode;

    private T mRefreshableView;

    private FrameLayout mRefreshableViewWrapper;

    private LinearLayout mHeaderLayout;
    private LinearLayout mFooterLayout;

    private Interpolator mScrollAnimationInterpolator;
    private CustomEnums.AnimationStyle mLoadingAnimationStyle = CustomEnums.AnimationStyle.getDefaultStyle();
}
